package net.zoneland.knowledge.service.impl;

import net.zoneland.knowledge.dao.PartyOrgLeaderMapper;
import net.zoneland.knowledge.dao.PartyOrgMapper;
import net.zoneland.knowledge.dao.PartyUserLeaderMapper;
import net.zoneland.knowledge.enums.CmpDnEnum;
import net.zoneland.knowledge.enums.PurviewEnum;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.PartyOrg;
import net.zoneland.knowledge.model.PartyUser;
import net.zoneland.knowledge.service.PurviewService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class PurviewServiceImpl implements PurviewService {

    @Autowired
    private PartyUserLeaderMapper partyUserLeaderMapper;

    @Autowired
    private PartyOrgLeaderMapper partyOrgLeaderMapper;

    @Autowired
    private PartyOrgMapper partyOrgMapper;

    /**
     * 党组织领导职位里有“党委”的，说明属于党委成员
     */
    private static final String DW = "党委";

    /**
     *党组织领导职位里有“书记”的，说明属于党组织书记。如果是党委书记，属于党委。
     */
    private static final String SJ = "书记";

    /**
     *党组织名称有“党委办公室”的，并且是书记的，属于党办书记
     */
    private static final String DB = "党委办公室";

    @Override
    public List<PurviewEnum> getPurviewEnumsByUser(final LoginUserPojo userPojo) {
        final List<PurviewEnum> purviewEnumList = new ArrayList<>();
        purviewEnumList.add(PurviewEnum.ORDINARY);

        //判断是否是管理员，是的话，判断是省公司还是市公司
        final int num = partyUserLeaderMapper.getAdminNumByUser(userPojo.getUserID());
        final String companyDN = userPojo.getCompanyDN();
        final boolean isZmcc = StringUtils.equals(CmpDnEnum.ZMCC.getValue(), companyDN);
        if (num > 0) {
            purviewEnumList.add(isZmcc ? PurviewEnum.PROVINCE_LEADER : PurviewEnum.BRANCH_LEADER);
        }

        //判断是否是党委，是的话，判断是省公司还是市公司
        final String ygId = Optional.ofNullable(userPojo.getPartyUser())
                .map(PartyUser::getYgId)
                .orElseGet(() -> getYgId(userPojo.getUserIdCard()));
        final List<String> positions = partyOrgLeaderMapper.getPositionByYgId(ygId);

        //支部委员
        if (CollectionUtils.isNotEmpty(positions)) {
            purviewEnumList.add(PurviewEnum.BRANCH_COMMITTEE);
        }

        if (checkType(positions, DW)) {
            purviewEnumList.add(isZmcc ? PurviewEnum.PROVINCE_DW : PurviewEnum.BRANCH_DW);
        }

        //是否是干事
        final Boolean ganShi = userPojo.getGanShi();
        if (ganShi != null && ganShi) {
            purviewEnumList.add(PurviewEnum.GANSHI);
        }

        //是否是党组织书记，分公司党办书记，省公司党办书记
        if (checkType(positions, SJ)) {
            purviewEnumList.add(PurviewEnum.SECRETARY);
            final String orgName = Optional.ofNullable(userPojo.getPartyUser())
                    .map(PartyUser::getOrgUUID)
                    .map(partyOrgMapper::getOrgByUuid)
                    .map(PartyOrg::getOrgName)
                    .orElse("");
            if (StringUtils.contains(orgName, DB)) {
                purviewEnumList.add(isZmcc ? PurviewEnum.PROVINCE_PARTY_OFFICE_SECRETARY : PurviewEnum.BRANCH_PARTY_OFFICE_SECRETARY);
            }
        }
        //是书记还是普通党员
        return  purviewEnumList;
    }

    /**
     * 判断类型，是否是书记，是否是党委
     * @param positions
     * @param type
     * @return
     */
    private boolean checkType(final List<String> positions, final String type) {
        for(String position: positions) {
            if (StringUtils.contains(position, type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取员工id
     * @param ygId
     * @return
     */
    private String getYgId(final String ygId) {
        if (StringUtils.startsWith(ygId, "E")) {
            return ygId;
        }
        return "E00" + ygId;
    }
}
